package com.ys.ffms.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ys.ffms.common.Result;
import com.ys.ffms.mapper.UserMapper;
import com.ys.ffms.pojo.PO.UserPO;
import com.ys.ffms.pojo.User;
import com.ys.ffms.pojo.VO.UserExcelVO;
import com.ys.ffms.service.UserService;
import com.ys.ffms.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<UserExcelVO> findAll() {
        return userMapper.findAll();
    }

    @Override
    public User queryUserByName(String userName) {
        return userMapper.queryUserByName(userName);
    }

    @Override
    public String checkPwd(String userName) {
        return userMapper.checkPwd(userName);
    }

    @Override
    public User findUserById(Integer id) {
        return userMapper.findUserById(id);
    }

    @Override
    public Result<?> onEdit(User user) {
        int i = userMapper.updateById(user);
        if(i>0){
            return Result.success(user);
        }else {
            return Result.error("500","修改失败!");
        }
    }

    @Override
    public Page<User> findAllBill(Integer pageNum, Integer pageSize, String name, String status) {
        Page<User> page = new Page<>(pageNum,pageSize);
       Page<User> page2 = userMapper.findAllByPage(page,name,status);
        return page2;
    }

    @Transactional
    @Override
    public int addUser(UserPO userPO) {
        //初始化user对象
        User user = new User();
        if(userPO==null){
            return 0;
        }
        //补全数据
        user.setName(userPO.getName());
        user.setSex(userPO.getSex());
        user.setAge(userPO.getAge());
        user.setUserId(userPO.getUserId());
        user.setPassword(MD5Util.string2MD5(userPO.getPassword()));
        user.setIdentity(userPO.getIdentity());
        user.setStatus(0);
        user.setCreateTime(new Date());
        int i = userMapper.insert(user);
        return i;
    }

    @Transactional
    @Override
    public Result<?> updateUserStatus(Integer id, Integer status) {
        System.err.println(id);//8
        System.err.println(status);//0
        //查出一整条数据
        User userById = userMapper.findUserById(id);
        //获取前台传来的状态
        int zt;
        if(status==1){
            //设置可用 0
            zt = 1;
        }else{
            //设置不可用 1
            zt = 0;
        }
        userMapper.updateStatusById(id,zt);
        return Result.success(userById,"修改成功!");
    }

    @Override
    public Result<User> delUserById(User user) {
        int i = userMapper.deleteById(user);
        if(i>0){
            //成功
            return Result.success(user,"删除成功!");
        }else{
            //失败
            return Result.error("500","删除失败!");
        }
    }

    @Transactional
    @Override
    public Result<?> onEditByEntity(UserPO userPO) {
        //实例化user
        User user = new User();
        //查询用户是否修改
        int result = userMapper.checkByPassword(userPO.getId(),userPO.getPassword());
        if(result>0){
            //未修改
            //补全数据
            user.setId(userPO.getId());
            user.setCreateTime(userPO.getCreateTime());
            user.setAge(userPO.getAge());
            user.setIdentity(userPO.getIdentity());
            user.setName(userPO.getName());
            user.setOutTime(userPO.getOutTime());
            user.setPassword(userPO.getPassword());
            user.setSex(userPO.getSex());
            user.setStatus(userPO.getStatus());
            user.setUserId(user.getUserId());
            //修改
            int i = userMapper.updateById(user);
            if(i>0){
                return Result.success(user,"修改成功!");
            }else{
                return Result.error("500","修改失败!");
            }
        }else {
            //修改密码
            //1.修改后的密码进行加密
            String passwordMD5 = MD5Util.string2MD5(userPO.getPassword());
            //补全数据
            user.setId(userPO.getId());
            user.setCreateTime(userPO.getCreateTime());
            user.setAge(userPO.getAge());
            user.setIdentity(userPO.getIdentity());
            user.setName(userPO.getName());
            user.setOutTime(userPO.getOutTime());
            user.setPassword(passwordMD5);
            user.setSex(userPO.getSex());
            user.setStatus(userPO.getStatus());
            user.setUserId(user.getUserId());

            //修改
            int i = userMapper.updateById(user);
            if(i>0){
                return Result.success(user,"修改成功!");
            }else{
                return Result.error("500","修改失败!");
            }
        }
    }

    @Override
    public int findAllMembers() {
        int count = userMapper.findAllMembers();
        return count;
    }

    @Override
    public int CheckUserIdAvailability(String id) {
        int i = userMapper.CheckUserIdAvailability(id);
        return i;
    }


}
